package com.sam4cloud.oauth2.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.sam4cloud.common.core.model.MPage;
import com.sam4cloud.common.core.model.MPageDTO;
import com.sam4cloud.common.core.model.R;
import com.sam4cloud.oauth2.project.model.dto.OauthAccountDTO;
import com.sam4cloud.oauth2.project.model.entity.OauthAccount;
import com.sam4cloud.oauth2.project.model.entity.OauthUser;
import com.sam4cloud.oauth2.project.model.vo.OauthAccountVO;
import com.sam4cloud.oauth2.project.service.IOauthAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 认证账户表 前端控制器
 * </p>
 *
 * @author Sam
 * @since 2022-12-05
 */
@Api(tags = "认证账户表")
@RestController
@RequestMapping("/oauthAccount")
public class OauthAccountRestController {

    @Autowired
    private IOauthAccountService oauthAccountService;

    @ApiOperation("分页查询账户")
    @GetMapping("/page")
    public R page(MPageDTO page) {
        MPJLambdaWrapper<OauthAccount> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(OauthAccount.class)
                .select(OauthUser::getPassword)
                .selectAs(OauthUser::getPhone, "test")
                .leftJoin(OauthUser.class, OauthUser::getDeptId, OauthAccount::getAppId);
        IPage<OauthAccountVO> res = oauthAccountService.selectJoinListPage(MPage.of(page), OauthAccountVO.class, wrapper);
        return R.ok(res);
    }

    @ApiOperation("新增账户")
    @PostMapping("/add")
    public R add(@RequestBody @Validated(OauthAccountDTO.Add.class) OauthAccountDTO oauthAccountDTO) {
        oauthAccountService.addAccount(oauthAccountDTO);
        return R.ok();
    }

}

